const  tools = require("./tools");
const  db = require("./db");
const  {upPic} = require("./upPic");
const mongodb = require("mongodb")
//添加商品
module.exports.addGoodsList=async function(req,res){
    const{ok,params} = await upPic(req,"goodsPic");
    // console.log(params,params.price)
    if(ok===1){
        const goodsType = await db.findOneById("goodsTypeList",params.goodsId);
        // console.log(goodsType)
        await db.insertOne("goodsList",{
            createTime:Date.now(),
            goodsName:params.goodsName,
            isTop:params.isTop === "true",
            ishot:params.ishot === "ture",
            goodsPrice:params.price/1,
            goodsPic:params.newPicName,
            goodsTypeName:goodsType.goodsTypeName,
            goodsTypeId:goodsType._id,
            shopTypeName:goodsType.shopTypeName,
            shopName:goodsType.shopName
        })
        tools.json(res,1,"插入成功")
    }else{
        tools.json(res);
    }
}
//获取商品
module.exports.getGoodsList=async function(req,res){
    let pageIndex = req.query.pageIndex/1;
    let pageSum = 1;
    let limit = 5;
    let whereObj = {};
    const count = await db.count("goodsList",whereObj);
    pageSum = Math.ceil(count/limit);
    if(pageSum < 1)
        pageSum = 1;
    if(pageIndex > pageSum)
        pageIndex = pageSum;
    if(pageIndex < 1)
        pageIndex = 1;
    const goodsList = await db.find("goodsList",{
        whereObj,
        sortObj:{
            createTime:-1
        },
        skip:(pageIndex-1)*limit,
        limit
    });
    // console.log(goodsList)
    res.json({
        ok:1,
        goodsList,
        pageIndex,
        pageSum
    })
}